package com.dizang.ddd.entity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dizang.ddd.business.IProjectCreator;
import com.dizang.ddd.constants.SrcEnum;

import lombok.Data;

@Data
public abstract class AbstractProjectBuilder implements IProjectCreator {

    private static final Logger logger = LoggerFactory.getLogger(AbstractProjectBuilder.class);

    protected String artifactId;

    protected String parentArtifactId;

    protected String groupId;
    
    protected List<AbstractProjectBuilder> dependencies = new ArrayList<>();

    protected List<String> packages;
    
    protected PomBuilder pomBuilder;
    

    public String getPath() {
        StringBuilder builder = new StringBuilder();
        builder.append("/");
        if (artifactId!=null) {
            builder.append(artifactId);
        }
        return builder.toString();
    }

    public boolean createSrc(String path) {
        String pathTemp = path + this.getPath();
        File proPath = new File(pathTemp);
        if (!proPath.exists()) {
            proPath.mkdirs();
        }
        File javaPath = new File(pathTemp + SrcEnum.JAVA_PATH.getValue());
        if (!javaPath.exists()) {
            javaPath.mkdirs();
        }
        File javaResourcesPath = new File(pathTemp + SrcEnum.JAVA_RESOURCES_PATH.getValue());
        if (!javaResourcesPath.exists()) {
            javaResourcesPath.mkdirs();
        }
        File testPath = new File(pathTemp + SrcEnum.TEST_PATH.getValue());
        if (!testPath.exists()) {
            testPath.mkdirs();
        }
        File testResourcesPath = new File(pathTemp + SrcEnum.TEST_RESOURCES_PATH.getValue());
        if (!testResourcesPath.exists()) {
            testResourcesPath.mkdirs();
        }
        return true;

    }
    
    protected void createPath(String path) {

        String pathTemp = path + this.getPath();
        File myPath = new File(pathTemp);
        if (!myPath.exists()) {
            myPath.mkdirs();
            logger.info("创建文件夹路径为:{}", pathTemp);
        }

    }

    public AbstractProjectBuilder addDependencies(AbstractProjectBuilder project) {
        this.getDependencies().add(project);
        return this;
    }

    protected void createPackage(String path) {
        String pathTemp = path + this.getPath();
        if (this.packages==null) {
            return ;
        }
        for (String str : this.packages) {
            File myPath = new File(pathTemp+SrcEnum.JAVA_PATH.getValue()+"/"+str);
            if (!myPath.exists()) {
                myPath.mkdirs();
                logger.info("创建文件夹路径为:{}", pathTemp);
            }
        }
        
    }


}
